package com.gb.models.user.controller;

import com.gb.models.user.model.SysUserEntity;
import com.gb.models.user.service.SysUserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.subject.Subject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * 用户控制器
 *
 * @author lgoodbook@163.com
 * @version 1.0.0
 * @Date 2017-08-04 13:35
 */
@RestController
public class SysUserController {

    protected Logger log = LoggerFactory.getLogger(SysUserController.class);

    @Resource
    private SysUserService userService;

    @RequestMapping("/user/list")
    @RequiresPermissions("user/list")
    public PageInfo userList(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                        @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                                        SysUserEntity userEntity){
        Subject subject = SecurityUtils.getSubject();
        if(subject.hasRole("user/list")){
            log.info("用户有查询列表权限");
        }else{
            log.info("用户没有查询列表权限");
        }
        log.info(pageNum+" === "+pageSize);
        PageHelper.startPage(pageNum, pageSize);
        List<SysUserEntity> userDoList = userService.queryUserList(userEntity);
        PageInfo pageInfo = new PageInfo(userDoList);
        return pageInfo;
    }

    @RequestMapping(value = "/user/save", method = RequestMethod.POST)
    @RequiresPermissions("user:save")
    public String saveUser(SysUserEntity userDo){
        userService.save(userDo);
        return "success";
    }

    @RequestMapping(value = "/user/modify", method = RequestMethod.POST)
    @RequiresPermissions("user:modify")
    public String modifyUser(SysUserEntity userDo){
        userService.updateUserById(userDo);
        return "success";
    }
}
